package com.store.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.easeui.widget.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.MemberInfoBean;

import java.util.List;


public class MemberListAdapter extends BaseAdapter {
    private Context context;        //上下文对象引用
    private List<MemberInfoBean> memberInfoBeanList;

    public MemberListAdapter(Context context, List<MemberInfoBean> memberInfoBeanList) {
        this.context = context;
        this.memberInfoBeanList = memberInfoBeanList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return memberInfoBeanList.size();
    }


    @Override
    public MemberInfoBean getItem(int position) {
        return memberInfoBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MemberInfoBean memberInfoBean = memberInfoBeanList.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_member_list, null);
            viewHolder.tv_nick_name = (TextView) convertView.findViewById(R.id.tv_nick_name);
            viewHolder.checkBox = (TextView) convertView.findViewById(R.id.cb);
            viewHolder.circleImageView = (CircleImageView) convertView.findViewById(R.id.avatar);
            convertView.setTag(viewHolder);            //表示给View添加一个格外的数据，
        } else {
            viewHolder = (ViewHolder) convertView.getTag();//通过getTag的方法将数据取出来
        }

//        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    memberInfoBean.setSelect(true);
//                } else {
//                    memberInfoBean.setSelect(false);
//                }
//            }
//        });

        if (memberInfoBean.isSelect()) {
            viewHolder.checkBox.setBackgroundResource(R.drawable.img_card_on);
        } else {
            viewHolder.checkBox.setBackgroundResource(R.drawable.img_card_off);
        }
        if(!TextUtils.isEmpty(memberInfoBean.getNick_name())){
            viewHolder.tv_nick_name.setText(memberInfoBean.getNick_name());
        }else{
            viewHolder.tv_nick_name.setText("匿名用户");
        }

        if(memberInfoBean.isSelect()){
            viewHolder.checkBox.setSelected(true);
        }else{
            viewHolder.checkBox.setSelected(false);
        }
        String url=memberInfoBean.getHead_pic_path();
        if(TextUtils.isEmpty(url)){
            url="null";
        }
        ImageLoader.getInstance().displayImage(url,viewHolder.circleImageView,options);
        Log.v("zyl","memberInfoBean.getNick_name()："+memberInfoBean.getNick_name());
        return convertView;
    }
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.ease_default_avatar)
            .showImageOnFail(R.drawable.ease_default_avatar)
            .showStubImage(R.drawable.ease_default_avatar)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
    public class ViewHolder {
        public TextView tv_nick_name;         //昵称
        public TextView checkBox;       //
        public CircleImageView circleImageView;
    }
}
